import { setStorageToken, getStorageToken } from '@/utils/storage'
import { getUserInfo } from '@/api/user'

export default {
  // 开启命名空间
  namespaced: true,
  state: {
    // 从localStorage中获取token
    token: getStorageToken(),
    userInfo: {}
  },
  getters: {},
  mutations: {
    // 本地永久存储token
    setToken(state, payload) {
      state.token = payload
      setStorageToken(payload)
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async Actions_getUserInfo(context) {
      try {
        const res = await getUserInfo()
        context.commit('setUserInfo', res.data)
      } catch {
        // this.$toast('获取用户信息失败')
        // // token过期 跳转到登录页
        // this.$router.push('/login')
      }
    }
  }
}
